
--处理callcenter模块拒接事件
--取出发其事件的坐席
local ccagent=event:getHeader("CC-Agent");

--取出事件中的callcenter里面的callid
local  callnum=event:getHeader("CC-Member-CID-Number");


--取出发起事件的名称
local action=event:getHeader("CC-Action");

--取出挂断的原因
local hupcase=event:getHeader("CC-Hangup-Cause");


local sessionuuid=event:getHeader("CC-Member-Session-UUID");

local recordfile=event:getHeader("variable_cc_record_filename");
--客户进入队列事件
if(action == "member-queue-start" ) then

	freeswitch.consoleLog("info","--member"..callnum.."进入队列");
	local api=freeswitch.API();
        local reply=api:executeString("lua  notify.lua "..callnum);
end



--客户出队列事件
if(action == "member-queue-end") then

		--发送给应用自定义事件
		freeswitch.consoleLog("info","--"..event:serialize());
                local recordfile=event:getHeader("variable_cc_record_filename");
                local event = freeswitch.Event("CUSTOM", "NECO:CALLCENTER_END");
                event:addHeader("Caller-Orig-Caller-ID-Number",callnum);
                event:addHeader("Caller-Callee-ID-Number",ccagent);
                event:addHeader("Unique-ID",sessionuuid);
                if(recordfile)then
                        event:addHeader("variable_cc_record_filename",""..recordfile)
                end;
                event:fire();

	local reason=event:getHeader("CC-Cancel-Reason");--得到结束的原因

	if(reason ==nil)then
		return "break";
	end	
	
	if(reason == "TIMEOUT")then--在队列超时无人接听
		local event = freeswitch.Event("CUSTOM", "NECO:NOAGENT");
        	event:addHeader("Caller-Orig-Caller-ID-Number",callnum);
	        event:addHeader("Unique-ID",sessionuuid);
	        event:fire();
	        freeswitch.consoleLog("info","处理拒绝事件完毕"..reason);	
	
	elseif(reason=="BREAK_OUT")then--在队列自己退出
		
		 local event = freeswitch.Event("CUSTOM", "NECO:SELFOUT");
        	event:addHeader("Caller-Orig-Caller-ID-Number",callnum);
	        event:addHeader("Unique-ID",sessionuuid);
        	event:fire();
	        freeswitch.consoleLog("info","处理拒绝事件完毕"..reason);
	end
	
	--出队列
	freeswitch.consoleLog("info","--member"..callnum.."出去队列"..reason);
        local api=freeswitch.API();
        local reply=api:executeString("lua  notify.lua "..callnum);



end


--判断如果桥接失败并且原因是NO_USER_RESPONSE 就有可能是拒绝
if(action == "bridge-agent-fail") then

	
	if(hupcase =="NO_USER_RESPONSE" or hupcase =="USER_BUSY")then

        	local event = freeswitch.Event("CUSTOM", "NECO:REJECT");
	        event:addHeader("Caller-Orig-Caller-ID-Number",callnum);
        	event:addHeader("Caller-Callee-ID-Number",ccagent);
	        event:addHeader("Unique-ID",sessionuuid);
        	event:fire();
	        freeswitch.consoleLog("info","坐席拒绝接听"..ccagent);

		
		  freeswitch.consoleLog("info","桥接失败->"..hupcase);
	         local api=freeswitch.API();
        	 local cmd="uuid_kill "..sessionuuid;
	         freeswitch.consoleLog("info","执行脚本"..cmd);
        	 local reply=api:executeString(cmd);
	         freeswitch.consoleLog("info","执行结果"..reply);
	elseif(hupcase =="NO_ANSWER")  then
		local event = freeswitch.Event("CUSTOM", "NECO:NO_ANSWER");
                event:addHeader("Caller-Orig-Caller-ID-Number",callnum);
                event:addHeader("Caller-Callee-ID-Number",ccagent);
                event:addHeader("Unique-ID",sessionuuid);
                event:fire();
                freeswitch.consoleLog("info","坐席没接听"..ccagent);

	
		  freeswitch.consoleLog("info","桥接失败->"..hupcase);
	         local api=freeswitch.API();
        	 local cmd="uuid_kill "..sessionuuid;
	         freeswitch.consoleLog("info","执行脚本"..cmd);
        	 local reply=api:executeString(cmd);
	         freeswitch.consoleLog("info","执行结果"..reply);
	end
	

	
end;





--坐席状态变更事件
if(action == "agent-status-change")then
	local domain=freeswitch.getGlobalVariable("domain");
		--状态一旦改变就通知给这个坐席
	     local status=event:getHeader("CC-Agent-Status");	
	     local event = freeswitch.Event("CUSTOM", "SMS::SEND_MESSAGE");
                   event:addHeader("proto", "sip");
                   event:addHeader("dest_proto", "sip");
                   event:addHeader("from", "1000");
                   event:addHeader("from_full", "sip:1000@"..domain)
                   event:addHeader("to",ccagent.."@"..domain);--接收者
                   event:addHeader("subject", "sip:"..ccagent.."@"..domain);
                   event:addHeader("type", "text/html");
                   event:addHeader("hint", "the hint");
                   event:addHeader("replying", "true");
                   event:addHeader("sip_profile", "internal");
		   event:addBody("{\"type\":\"STATUS_CHANGE\",\"data\":\""..status.."\"}");
                   event:fire();
end




